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