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