see http://mbed.org/users/okini3939/notebook/wattmeter-shield-on-mbed/
Fork of GSwifi_xively by
GSwifiInterface/GSwifi/sha1.cpp@4:9a2415f2ab07, 2013-11-27 (annotated)
- Committer:
- okini3939
- Date:
- Wed Nov 27 08:18:45 2013 +0000
- Revision:
- 4:9a2415f2ab07
update GSwifiInterface library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
okini3939 | 4:9a2415f2ab07 | 1 | /* |
okini3939 | 4:9a2415f2ab07 | 2 | * source from http://www.ipa.go.jp/security/rfc/RFC3174JA.html |
okini3939 | 4:9a2415f2ab07 | 3 | */ |
okini3939 | 4:9a2415f2ab07 | 4 | #include "sha1.h" |
okini3939 | 4:9a2415f2ab07 | 5 | |
okini3939 | 4:9a2415f2ab07 | 6 | /* |
okini3939 | 4:9a2415f2ab07 | 7 | * Define the SHA1 circular left shift macro |
okini3939 | 4:9a2415f2ab07 | 8 | */ |
okini3939 | 4:9a2415f2ab07 | 9 | #define SHA1CircularShift(bits,word) \ |
okini3939 | 4:9a2415f2ab07 | 10 | (((word) << (bits)) | ((word) >> (32-(bits)))) |
okini3939 | 4:9a2415f2ab07 | 11 | |
okini3939 | 4:9a2415f2ab07 | 12 | /* Local Function Prototyptes */ |
okini3939 | 4:9a2415f2ab07 | 13 | void SHA1PadMessage(SHA1Context *); |
okini3939 | 4:9a2415f2ab07 | 14 | void SHA1ProcessMessageBlock(SHA1Context *); |
okini3939 | 4:9a2415f2ab07 | 15 | |
okini3939 | 4:9a2415f2ab07 | 16 | /* |
okini3939 | 4:9a2415f2ab07 | 17 | * SHA1Reset |
okini3939 | 4:9a2415f2ab07 | 18 | * |
okini3939 | 4:9a2415f2ab07 | 19 | * Description: |
okini3939 | 4:9a2415f2ab07 | 20 | * This function will initialize the SHA1Context in preparation |
okini3939 | 4:9a2415f2ab07 | 21 | * for computing a new SHA1 message digest. |
okini3939 | 4:9a2415f2ab07 | 22 | * |
okini3939 | 4:9a2415f2ab07 | 23 | * Parameters: |
okini3939 | 4:9a2415f2ab07 | 24 | * context: [in/out] |
okini3939 | 4:9a2415f2ab07 | 25 | * The context to reset. |
okini3939 | 4:9a2415f2ab07 | 26 | * |
okini3939 | 4:9a2415f2ab07 | 27 | * Returns: |
okini3939 | 4:9a2415f2ab07 | 28 | * sha Error Code. |
okini3939 | 4:9a2415f2ab07 | 29 | * |
okini3939 | 4:9a2415f2ab07 | 30 | */ |
okini3939 | 4:9a2415f2ab07 | 31 | int SHA1Reset(SHA1Context *context) |
okini3939 | 4:9a2415f2ab07 | 32 | { |
okini3939 | 4:9a2415f2ab07 | 33 | if (!context) |
okini3939 | 4:9a2415f2ab07 | 34 | { |
okini3939 | 4:9a2415f2ab07 | 35 | return shaNull; |
okini3939 | 4:9a2415f2ab07 | 36 | } |
okini3939 | 4:9a2415f2ab07 | 37 | |
okini3939 | 4:9a2415f2ab07 | 38 | context->Length_Low = 0; |
okini3939 | 4:9a2415f2ab07 | 39 | context->Length_High = 0; |
okini3939 | 4:9a2415f2ab07 | 40 | context->Message_Block_Index = 0; |
okini3939 | 4:9a2415f2ab07 | 41 | |
okini3939 | 4:9a2415f2ab07 | 42 | context->Intermediate_Hash[0] = 0x67452301; |
okini3939 | 4:9a2415f2ab07 | 43 | context->Intermediate_Hash[1] = 0xEFCDAB89; |
okini3939 | 4:9a2415f2ab07 | 44 | context->Intermediate_Hash[2] = 0x98BADCFE; |
okini3939 | 4:9a2415f2ab07 | 45 | context->Intermediate_Hash[3] = 0x10325476; |
okini3939 | 4:9a2415f2ab07 | 46 | context->Intermediate_Hash[4] = 0xC3D2E1F0; |
okini3939 | 4:9a2415f2ab07 | 47 | |
okini3939 | 4:9a2415f2ab07 | 48 | context->Computed = 0; |
okini3939 | 4:9a2415f2ab07 | 49 | context->Corrupted = 0; |
okini3939 | 4:9a2415f2ab07 | 50 | |
okini3939 | 4:9a2415f2ab07 | 51 | return shaSuccess; |
okini3939 | 4:9a2415f2ab07 | 52 | } |
okini3939 | 4:9a2415f2ab07 | 53 | |
okini3939 | 4:9a2415f2ab07 | 54 | /* |
okini3939 | 4:9a2415f2ab07 | 55 | * SHA1Result |
okini3939 | 4:9a2415f2ab07 | 56 | * |
okini3939 | 4:9a2415f2ab07 | 57 | * Description: |
okini3939 | 4:9a2415f2ab07 | 58 | * This function will return the 160-bit message digest into the |
okini3939 | 4:9a2415f2ab07 | 59 | * Message_Digest array provided by the caller. |
okini3939 | 4:9a2415f2ab07 | 60 | * NOTE: The first octet of hash is stored in the 0th element, |
okini3939 | 4:9a2415f2ab07 | 61 | * the last octet of hash in the 19th element. |
okini3939 | 4:9a2415f2ab07 | 62 | * |
okini3939 | 4:9a2415f2ab07 | 63 | * Parameters: |
okini3939 | 4:9a2415f2ab07 | 64 | * context: [in/out] |
okini3939 | 4:9a2415f2ab07 | 65 | * The context to use to calculate the SHA-1 hash. |
okini3939 | 4:9a2415f2ab07 | 66 | * Message_Digest: [out] |
okini3939 | 4:9a2415f2ab07 | 67 | * Where the digest is returned. |
okini3939 | 4:9a2415f2ab07 | 68 | * |
okini3939 | 4:9a2415f2ab07 | 69 | * Returns: |
okini3939 | 4:9a2415f2ab07 | 70 | * sha Error Code. |
okini3939 | 4:9a2415f2ab07 | 71 | * |
okini3939 | 4:9a2415f2ab07 | 72 | */ |
okini3939 | 4:9a2415f2ab07 | 73 | int SHA1Result( SHA1Context *context, |
okini3939 | 4:9a2415f2ab07 | 74 | uint8_t Message_Digest[SHA1HashSize]) |
okini3939 | 4:9a2415f2ab07 | 75 | { |
okini3939 | 4:9a2415f2ab07 | 76 | int i; |
okini3939 | 4:9a2415f2ab07 | 77 | |
okini3939 | 4:9a2415f2ab07 | 78 | if (!context || !Message_Digest) |
okini3939 | 4:9a2415f2ab07 | 79 | { |
okini3939 | 4:9a2415f2ab07 | 80 | return shaNull; |
okini3939 | 4:9a2415f2ab07 | 81 | } |
okini3939 | 4:9a2415f2ab07 | 82 | |
okini3939 | 4:9a2415f2ab07 | 83 | if (context->Corrupted) |
okini3939 | 4:9a2415f2ab07 | 84 | { |
okini3939 | 4:9a2415f2ab07 | 85 | return context->Corrupted; |
okini3939 | 4:9a2415f2ab07 | 86 | } |
okini3939 | 4:9a2415f2ab07 | 87 | |
okini3939 | 4:9a2415f2ab07 | 88 | if (!context->Computed) |
okini3939 | 4:9a2415f2ab07 | 89 | { |
okini3939 | 4:9a2415f2ab07 | 90 | SHA1PadMessage(context); |
okini3939 | 4:9a2415f2ab07 | 91 | for(i=0; i<64; ++i) |
okini3939 | 4:9a2415f2ab07 | 92 | { |
okini3939 | 4:9a2415f2ab07 | 93 | /* message may be sensitive, clear it out */ |
okini3939 | 4:9a2415f2ab07 | 94 | context->Message_Block[i] = 0; |
okini3939 | 4:9a2415f2ab07 | 95 | } |
okini3939 | 4:9a2415f2ab07 | 96 | context->Length_Low = 0; /* and clear length */ |
okini3939 | 4:9a2415f2ab07 | 97 | context->Length_High = 0; |
okini3939 | 4:9a2415f2ab07 | 98 | context->Computed = 1; |
okini3939 | 4:9a2415f2ab07 | 99 | } |
okini3939 | 4:9a2415f2ab07 | 100 | |
okini3939 | 4:9a2415f2ab07 | 101 | for(i = 0; i < SHA1HashSize; ++i) |
okini3939 | 4:9a2415f2ab07 | 102 | { |
okini3939 | 4:9a2415f2ab07 | 103 | Message_Digest[i] = context->Intermediate_Hash[i>>2] |
okini3939 | 4:9a2415f2ab07 | 104 | >> 8 * ( 3 - ( i & 0x03 ) ); |
okini3939 | 4:9a2415f2ab07 | 105 | } |
okini3939 | 4:9a2415f2ab07 | 106 | |
okini3939 | 4:9a2415f2ab07 | 107 | return shaSuccess; |
okini3939 | 4:9a2415f2ab07 | 108 | } |
okini3939 | 4:9a2415f2ab07 | 109 | |
okini3939 | 4:9a2415f2ab07 | 110 | /* |
okini3939 | 4:9a2415f2ab07 | 111 | * SHA1Input |
okini3939 | 4:9a2415f2ab07 | 112 | * |
okini3939 | 4:9a2415f2ab07 | 113 | * Description: |
okini3939 | 4:9a2415f2ab07 | 114 | * This function accepts an array of octets as the next portion |
okini3939 | 4:9a2415f2ab07 | 115 | * of the message. |
okini3939 | 4:9a2415f2ab07 | 116 | * |
okini3939 | 4:9a2415f2ab07 | 117 | * Parameters: |
okini3939 | 4:9a2415f2ab07 | 118 | * context: [in/out] |
okini3939 | 4:9a2415f2ab07 | 119 | * The SHA context to update |
okini3939 | 4:9a2415f2ab07 | 120 | * message_array: [in] |
okini3939 | 4:9a2415f2ab07 | 121 | * An array of characters representing the next portion of |
okini3939 | 4:9a2415f2ab07 | 122 | * the message. |
okini3939 | 4:9a2415f2ab07 | 123 | * length: [in] |
okini3939 | 4:9a2415f2ab07 | 124 | * The length of the message in message_array |
okini3939 | 4:9a2415f2ab07 | 125 | * |
okini3939 | 4:9a2415f2ab07 | 126 | * Returns: |
okini3939 | 4:9a2415f2ab07 | 127 | * sha Error Code. |
okini3939 | 4:9a2415f2ab07 | 128 | * |
okini3939 | 4:9a2415f2ab07 | 129 | */ |
okini3939 | 4:9a2415f2ab07 | 130 | int SHA1Input( SHA1Context *context, |
okini3939 | 4:9a2415f2ab07 | 131 | const uint8_t *message_array, |
okini3939 | 4:9a2415f2ab07 | 132 | unsigned length) |
okini3939 | 4:9a2415f2ab07 | 133 | { |
okini3939 | 4:9a2415f2ab07 | 134 | if (!length) |
okini3939 | 4:9a2415f2ab07 | 135 | { |
okini3939 | 4:9a2415f2ab07 | 136 | return shaSuccess; |
okini3939 | 4:9a2415f2ab07 | 137 | } |
okini3939 | 4:9a2415f2ab07 | 138 | |
okini3939 | 4:9a2415f2ab07 | 139 | if (!context || !message_array) |
okini3939 | 4:9a2415f2ab07 | 140 | { |
okini3939 | 4:9a2415f2ab07 | 141 | return shaNull; |
okini3939 | 4:9a2415f2ab07 | 142 | } |
okini3939 | 4:9a2415f2ab07 | 143 | |
okini3939 | 4:9a2415f2ab07 | 144 | if (context->Computed) |
okini3939 | 4:9a2415f2ab07 | 145 | { |
okini3939 | 4:9a2415f2ab07 | 146 | context->Corrupted = shaStateError; |
okini3939 | 4:9a2415f2ab07 | 147 | return shaStateError; |
okini3939 | 4:9a2415f2ab07 | 148 | } |
okini3939 | 4:9a2415f2ab07 | 149 | |
okini3939 | 4:9a2415f2ab07 | 150 | if (context->Corrupted) |
okini3939 | 4:9a2415f2ab07 | 151 | { |
okini3939 | 4:9a2415f2ab07 | 152 | return context->Corrupted; |
okini3939 | 4:9a2415f2ab07 | 153 | } |
okini3939 | 4:9a2415f2ab07 | 154 | while(length-- && !context->Corrupted) |
okini3939 | 4:9a2415f2ab07 | 155 | { |
okini3939 | 4:9a2415f2ab07 | 156 | context->Message_Block[context->Message_Block_Index++] = |
okini3939 | 4:9a2415f2ab07 | 157 | (*message_array & 0xFF); |
okini3939 | 4:9a2415f2ab07 | 158 | |
okini3939 | 4:9a2415f2ab07 | 159 | context->Length_Low += 8; |
okini3939 | 4:9a2415f2ab07 | 160 | if (context->Length_Low == 0) |
okini3939 | 4:9a2415f2ab07 | 161 | { |
okini3939 | 4:9a2415f2ab07 | 162 | context->Length_High++; |
okini3939 | 4:9a2415f2ab07 | 163 | if (context->Length_High == 0) |
okini3939 | 4:9a2415f2ab07 | 164 | { |
okini3939 | 4:9a2415f2ab07 | 165 | /* Message is too long */ |
okini3939 | 4:9a2415f2ab07 | 166 | context->Corrupted = 1; |
okini3939 | 4:9a2415f2ab07 | 167 | } |
okini3939 | 4:9a2415f2ab07 | 168 | } |
okini3939 | 4:9a2415f2ab07 | 169 | |
okini3939 | 4:9a2415f2ab07 | 170 | if (context->Message_Block_Index == 64) |
okini3939 | 4:9a2415f2ab07 | 171 | { |
okini3939 | 4:9a2415f2ab07 | 172 | SHA1ProcessMessageBlock(context); |
okini3939 | 4:9a2415f2ab07 | 173 | } |
okini3939 | 4:9a2415f2ab07 | 174 | |
okini3939 | 4:9a2415f2ab07 | 175 | message_array++; |
okini3939 | 4:9a2415f2ab07 | 176 | } |
okini3939 | 4:9a2415f2ab07 | 177 | |
okini3939 | 4:9a2415f2ab07 | 178 | return shaSuccess; |
okini3939 | 4:9a2415f2ab07 | 179 | } |
okini3939 | 4:9a2415f2ab07 | 180 | |
okini3939 | 4:9a2415f2ab07 | 181 | /* |
okini3939 | 4:9a2415f2ab07 | 182 | * SHA1ProcessMessageBlock |
okini3939 | 4:9a2415f2ab07 | 183 | * |
okini3939 | 4:9a2415f2ab07 | 184 | * Description: |
okini3939 | 4:9a2415f2ab07 | 185 | * This function will process the next 512 bits of the message |
okini3939 | 4:9a2415f2ab07 | 186 | * stored in the Message_Block array. |
okini3939 | 4:9a2415f2ab07 | 187 | * |
okini3939 | 4:9a2415f2ab07 | 188 | * Parameters: |
okini3939 | 4:9a2415f2ab07 | 189 | * None. |
okini3939 | 4:9a2415f2ab07 | 190 | * |
okini3939 | 4:9a2415f2ab07 | 191 | * Returns: |
okini3939 | 4:9a2415f2ab07 | 192 | * Nothing. |
okini3939 | 4:9a2415f2ab07 | 193 | * |
okini3939 | 4:9a2415f2ab07 | 194 | * Comments: |
okini3939 | 4:9a2415f2ab07 | 195 | * Many of the variable names in this code, especially the |
okini3939 | 4:9a2415f2ab07 | 196 | * single character names, were used because those were the |
okini3939 | 4:9a2415f2ab07 | 197 | * names used in the publication. |
okini3939 | 4:9a2415f2ab07 | 198 | * |
okini3939 | 4:9a2415f2ab07 | 199 | * |
okini3939 | 4:9a2415f2ab07 | 200 | */ |
okini3939 | 4:9a2415f2ab07 | 201 | void SHA1ProcessMessageBlock(SHA1Context *context) |
okini3939 | 4:9a2415f2ab07 | 202 | { |
okini3939 | 4:9a2415f2ab07 | 203 | const uint32_t K[] = { /* Constants defined in SHA-1 */ |
okini3939 | 4:9a2415f2ab07 | 204 | 0x5A827999, |
okini3939 | 4:9a2415f2ab07 | 205 | 0x6ED9EBA1, |
okini3939 | 4:9a2415f2ab07 | 206 | 0x8F1BBCDC, |
okini3939 | 4:9a2415f2ab07 | 207 | 0xCA62C1D6 |
okini3939 | 4:9a2415f2ab07 | 208 | }; |
okini3939 | 4:9a2415f2ab07 | 209 | int t; /* Loop counter */ |
okini3939 | 4:9a2415f2ab07 | 210 | uint32_t temp; /* Temporary word value */ |
okini3939 | 4:9a2415f2ab07 | 211 | uint32_t W[80]; /* Word sequence */ |
okini3939 | 4:9a2415f2ab07 | 212 | uint32_t A, B, C, D, E; /* Word buffers */ |
okini3939 | 4:9a2415f2ab07 | 213 | |
okini3939 | 4:9a2415f2ab07 | 214 | /* |
okini3939 | 4:9a2415f2ab07 | 215 | * Initialize the first 16 words in the array W |
okini3939 | 4:9a2415f2ab07 | 216 | */ |
okini3939 | 4:9a2415f2ab07 | 217 | for(t = 0; t < 16; t++) |
okini3939 | 4:9a2415f2ab07 | 218 | { |
okini3939 | 4:9a2415f2ab07 | 219 | W[t] = context->Message_Block[t * 4] << 24; |
okini3939 | 4:9a2415f2ab07 | 220 | W[t] |= context->Message_Block[t * 4 + 1] << 16; |
okini3939 | 4:9a2415f2ab07 | 221 | W[t] |= context->Message_Block[t * 4 + 2] << 8; |
okini3939 | 4:9a2415f2ab07 | 222 | W[t] |= context->Message_Block[t * 4 + 3]; |
okini3939 | 4:9a2415f2ab07 | 223 | } |
okini3939 | 4:9a2415f2ab07 | 224 | |
okini3939 | 4:9a2415f2ab07 | 225 | for(t = 16; t < 80; t++) |
okini3939 | 4:9a2415f2ab07 | 226 | { |
okini3939 | 4:9a2415f2ab07 | 227 | W[t] = SHA1CircularShift(1,W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16]); |
okini3939 | 4:9a2415f2ab07 | 228 | } |
okini3939 | 4:9a2415f2ab07 | 229 | |
okini3939 | 4:9a2415f2ab07 | 230 | A = context->Intermediate_Hash[0]; |
okini3939 | 4:9a2415f2ab07 | 231 | B = context->Intermediate_Hash[1]; |
okini3939 | 4:9a2415f2ab07 | 232 | C = context->Intermediate_Hash[2]; |
okini3939 | 4:9a2415f2ab07 | 233 | D = context->Intermediate_Hash[3]; |
okini3939 | 4:9a2415f2ab07 | 234 | E = context->Intermediate_Hash[4]; |
okini3939 | 4:9a2415f2ab07 | 235 | |
okini3939 | 4:9a2415f2ab07 | 236 | for(t = 0; t < 20; t++) |
okini3939 | 4:9a2415f2ab07 | 237 | { |
okini3939 | 4:9a2415f2ab07 | 238 | temp = SHA1CircularShift(5,A) + |
okini3939 | 4:9a2415f2ab07 | 239 | ((B & C) | ((~B) & D)) + E + W[t] + K[0]; |
okini3939 | 4:9a2415f2ab07 | 240 | E = D; |
okini3939 | 4:9a2415f2ab07 | 241 | D = C; |
okini3939 | 4:9a2415f2ab07 | 242 | C = SHA1CircularShift(30,B); |
okini3939 | 4:9a2415f2ab07 | 243 | B = A; |
okini3939 | 4:9a2415f2ab07 | 244 | A = temp; |
okini3939 | 4:9a2415f2ab07 | 245 | } |
okini3939 | 4:9a2415f2ab07 | 246 | |
okini3939 | 4:9a2415f2ab07 | 247 | for(t = 20; t < 40; t++) |
okini3939 | 4:9a2415f2ab07 | 248 | { |
okini3939 | 4:9a2415f2ab07 | 249 | temp = SHA1CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[1]; |
okini3939 | 4:9a2415f2ab07 | 250 | E = D; |
okini3939 | 4:9a2415f2ab07 | 251 | D = C; |
okini3939 | 4:9a2415f2ab07 | 252 | C = SHA1CircularShift(30,B); |
okini3939 | 4:9a2415f2ab07 | 253 | B = A; |
okini3939 | 4:9a2415f2ab07 | 254 | A = temp; |
okini3939 | 4:9a2415f2ab07 | 255 | } |
okini3939 | 4:9a2415f2ab07 | 256 | |
okini3939 | 4:9a2415f2ab07 | 257 | for(t = 40; t < 60; t++) |
okini3939 | 4:9a2415f2ab07 | 258 | { |
okini3939 | 4:9a2415f2ab07 | 259 | temp = SHA1CircularShift(5,A) + |
okini3939 | 4:9a2415f2ab07 | 260 | ((B & C) | (B & D) | (C & D)) + E + W[t] + K[2]; |
okini3939 | 4:9a2415f2ab07 | 261 | E = D; |
okini3939 | 4:9a2415f2ab07 | 262 | D = C; |
okini3939 | 4:9a2415f2ab07 | 263 | C = SHA1CircularShift(30,B); |
okini3939 | 4:9a2415f2ab07 | 264 | B = A; |
okini3939 | 4:9a2415f2ab07 | 265 | A = temp; |
okini3939 | 4:9a2415f2ab07 | 266 | } |
okini3939 | 4:9a2415f2ab07 | 267 | |
okini3939 | 4:9a2415f2ab07 | 268 | for(t = 60; t < 80; t++) |
okini3939 | 4:9a2415f2ab07 | 269 | { |
okini3939 | 4:9a2415f2ab07 | 270 | temp = SHA1CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[3]; |
okini3939 | 4:9a2415f2ab07 | 271 | E = D; |
okini3939 | 4:9a2415f2ab07 | 272 | D = C; |
okini3939 | 4:9a2415f2ab07 | 273 | C = SHA1CircularShift(30,B); |
okini3939 | 4:9a2415f2ab07 | 274 | B = A; |
okini3939 | 4:9a2415f2ab07 | 275 | A = temp; |
okini3939 | 4:9a2415f2ab07 | 276 | } |
okini3939 | 4:9a2415f2ab07 | 277 | |
okini3939 | 4:9a2415f2ab07 | 278 | context->Intermediate_Hash[0] += A; |
okini3939 | 4:9a2415f2ab07 | 279 | context->Intermediate_Hash[1] += B; |
okini3939 | 4:9a2415f2ab07 | 280 | context->Intermediate_Hash[2] += C; |
okini3939 | 4:9a2415f2ab07 | 281 | context->Intermediate_Hash[3] += D; |
okini3939 | 4:9a2415f2ab07 | 282 | context->Intermediate_Hash[4] += E; |
okini3939 | 4:9a2415f2ab07 | 283 | |
okini3939 | 4:9a2415f2ab07 | 284 | context->Message_Block_Index = 0; |
okini3939 | 4:9a2415f2ab07 | 285 | } |
okini3939 | 4:9a2415f2ab07 | 286 | |
okini3939 | 4:9a2415f2ab07 | 287 | |
okini3939 | 4:9a2415f2ab07 | 288 | /* |
okini3939 | 4:9a2415f2ab07 | 289 | * SHA1PadMessage |
okini3939 | 4:9a2415f2ab07 | 290 | * |
okini3939 | 4:9a2415f2ab07 | 291 | * Description: |
okini3939 | 4:9a2415f2ab07 | 292 | * According to the standard, the message must be padded to an even |
okini3939 | 4:9a2415f2ab07 | 293 | * 512 bits. The first padding bit must be a '1'. The last 64 |
okini3939 | 4:9a2415f2ab07 | 294 | * bits represent the length of the original message. All bits in |
okini3939 | 4:9a2415f2ab07 | 295 | * between should be 0. This function will pad the message |
okini3939 | 4:9a2415f2ab07 | 296 | * according to those rules by filling the Message_Block array |
okini3939 | 4:9a2415f2ab07 | 297 | * accordingly. It will also call the ProcessMessageBlock function |
okini3939 | 4:9a2415f2ab07 | 298 | * provided appropriately. When it returns, it can be assumed that |
okini3939 | 4:9a2415f2ab07 | 299 | * the message digest has been computed. |
okini3939 | 4:9a2415f2ab07 | 300 | * |
okini3939 | 4:9a2415f2ab07 | 301 | * Parameters: |
okini3939 | 4:9a2415f2ab07 | 302 | * context: [in/out] |
okini3939 | 4:9a2415f2ab07 | 303 | * The context to pad |
okini3939 | 4:9a2415f2ab07 | 304 | * ProcessMessageBlock: [in] |
okini3939 | 4:9a2415f2ab07 | 305 | * The appropriate SHA*ProcessMessageBlock function |
okini3939 | 4:9a2415f2ab07 | 306 | * Returns: |
okini3939 | 4:9a2415f2ab07 | 307 | * Nothing. |
okini3939 | 4:9a2415f2ab07 | 308 | * |
okini3939 | 4:9a2415f2ab07 | 309 | */ |
okini3939 | 4:9a2415f2ab07 | 310 | |
okini3939 | 4:9a2415f2ab07 | 311 | void SHA1PadMessage(SHA1Context *context) |
okini3939 | 4:9a2415f2ab07 | 312 | { |
okini3939 | 4:9a2415f2ab07 | 313 | /* |
okini3939 | 4:9a2415f2ab07 | 314 | * Check to see if the current message block is too small to hold |
okini3939 | 4:9a2415f2ab07 | 315 | * the initial padding bits and length. If so, we will pad the |
okini3939 | 4:9a2415f2ab07 | 316 | * block, process it, and then continue padding into a second |
okini3939 | 4:9a2415f2ab07 | 317 | * block. |
okini3939 | 4:9a2415f2ab07 | 318 | */ |
okini3939 | 4:9a2415f2ab07 | 319 | if (context->Message_Block_Index > 55) |
okini3939 | 4:9a2415f2ab07 | 320 | { |
okini3939 | 4:9a2415f2ab07 | 321 | context->Message_Block[context->Message_Block_Index++] = 0x80; |
okini3939 | 4:9a2415f2ab07 | 322 | while(context->Message_Block_Index < 64) |
okini3939 | 4:9a2415f2ab07 | 323 | { |
okini3939 | 4:9a2415f2ab07 | 324 | context->Message_Block[context->Message_Block_Index++] = 0; |
okini3939 | 4:9a2415f2ab07 | 325 | } |
okini3939 | 4:9a2415f2ab07 | 326 | |
okini3939 | 4:9a2415f2ab07 | 327 | SHA1ProcessMessageBlock(context); |
okini3939 | 4:9a2415f2ab07 | 328 | |
okini3939 | 4:9a2415f2ab07 | 329 | while(context->Message_Block_Index < 56) |
okini3939 | 4:9a2415f2ab07 | 330 | { |
okini3939 | 4:9a2415f2ab07 | 331 | context->Message_Block[context->Message_Block_Index++] = 0; |
okini3939 | 4:9a2415f2ab07 | 332 | } |
okini3939 | 4:9a2415f2ab07 | 333 | } |
okini3939 | 4:9a2415f2ab07 | 334 | else |
okini3939 | 4:9a2415f2ab07 | 335 | { |
okini3939 | 4:9a2415f2ab07 | 336 | context->Message_Block[context->Message_Block_Index++] = 0x80; |
okini3939 | 4:9a2415f2ab07 | 337 | while(context->Message_Block_Index < 56) |
okini3939 | 4:9a2415f2ab07 | 338 | { |
okini3939 | 4:9a2415f2ab07 | 339 | context->Message_Block[context->Message_Block_Index++] = 0; |
okini3939 | 4:9a2415f2ab07 | 340 | } |
okini3939 | 4:9a2415f2ab07 | 341 | } |
okini3939 | 4:9a2415f2ab07 | 342 | |
okini3939 | 4:9a2415f2ab07 | 343 | /* |
okini3939 | 4:9a2415f2ab07 | 344 | * Store the message length as the last 8 octets |
okini3939 | 4:9a2415f2ab07 | 345 | */ |
okini3939 | 4:9a2415f2ab07 | 346 | context->Message_Block[56] = context->Length_High >> 24; |
okini3939 | 4:9a2415f2ab07 | 347 | context->Message_Block[57] = context->Length_High >> 16; |
okini3939 | 4:9a2415f2ab07 | 348 | context->Message_Block[58] = context->Length_High >> 8; |
okini3939 | 4:9a2415f2ab07 | 349 | context->Message_Block[59] = context->Length_High; |
okini3939 | 4:9a2415f2ab07 | 350 | context->Message_Block[60] = context->Length_Low >> 24; |
okini3939 | 4:9a2415f2ab07 | 351 | context->Message_Block[61] = context->Length_Low >> 16; |
okini3939 | 4:9a2415f2ab07 | 352 | context->Message_Block[62] = context->Length_Low >> 8; |
okini3939 | 4:9a2415f2ab07 | 353 | context->Message_Block[63] = context->Length_Low; |
okini3939 | 4:9a2415f2ab07 | 354 | |
okini3939 | 4:9a2415f2ab07 | 355 | SHA1ProcessMessageBlock(context); |
okini3939 | 4:9a2415f2ab07 | 356 | } |
okini3939 | 4:9a2415f2ab07 | 357 | |
okini3939 | 4:9a2415f2ab07 | 358 | void sha1 (const char *input, int len, char *output) { |
okini3939 | 4:9a2415f2ab07 | 359 | SHA1Context sha; |
okini3939 | 4:9a2415f2ab07 | 360 | |
okini3939 | 4:9a2415f2ab07 | 361 | SHA1Reset(&sha); |
okini3939 | 4:9a2415f2ab07 | 362 | SHA1Input(&sha, (unsigned char*)input, len); |
okini3939 | 4:9a2415f2ab07 | 363 | SHA1Result(&sha, (uint8_t*)output); |
okini3939 | 4:9a2415f2ab07 | 364 | } |