Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <string.h> |
Raphael Moll | 4605b3f | 2012-02-03 23:02:33 -0800 | [diff] [blame] | 18 | |
| 19 | #ifdef USE_MINGW |
| 20 | #include <winsock2.h> |
| 21 | #else |
Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 22 | #include <arpa/inet.h> |
Raphael Moll | 4605b3f | 2012-02-03 23:02:33 -0800 | [diff] [blame] | 23 | #endif |
Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 24 | |
| 25 | #include "ext4_utils.h" |
| 26 | #include "sha1.h" |
| 27 | #include "uuid.h" |
| 28 | |
Ken Sumrall | 107a9f1 | 2011-06-29 20:28:30 -0700 | [diff] [blame] | 29 | /* Definition from RFC-4122 */ |
Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 30 | struct uuid { |
| 31 | u32 time_low; |
Ken Sumrall | 107a9f1 | 2011-06-29 20:28:30 -0700 | [diff] [blame] | 32 | u16 time_mid; |
Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 33 | u16 time_hi_and_version; |
| 34 | u8 clk_seq_hi_res; |
| 35 | u8 clk_seq_low; |
| 36 | u16 node0_1; |
| 37 | u32 node2_5; |
| 38 | }; |
| 39 | |
| 40 | static void sha1_hash(const char *namespace, const char *name, |
Colin Cross | 8aef66d | 2010-06-20 23:22:12 -0700 | [diff] [blame] | 41 | unsigned char sha1[SHA1_DIGEST_LENGTH]) |
Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 42 | { |
| 43 | SHA1_CTX ctx; |
| 44 | SHA1Init(&ctx); |
| 45 | SHA1Update(&ctx, (const u8*)namespace, strlen(namespace)); |
| 46 | SHA1Update(&ctx, (const u8*)name, strlen(name)); |
| 47 | SHA1Final(sha1, &ctx); |
| 48 | } |
| 49 | |
| 50 | void generate_uuid(const char *namespace, const char *name, u8 result[16]) |
| 51 | { |
| 52 | unsigned char sha1[SHA1_DIGEST_LENGTH]; |
| 53 | struct uuid *uuid = (struct uuid *)result; |
| 54 | |
| 55 | sha1_hash(namespace, name, (unsigned char*)sha1); |
| 56 | memcpy(uuid, sha1, sizeof(struct uuid)); |
| 57 | |
| 58 | uuid->time_low = ntohl(uuid->time_low); |
| 59 | uuid->time_mid = ntohs(uuid->time_mid); |
| 60 | uuid->time_hi_and_version = ntohs(uuid->time_hi_and_version); |
| 61 | uuid->time_hi_and_version &= 0x0FFF; |
| 62 | uuid->time_hi_and_version |= (5 << 12); |
| 63 | uuid->clk_seq_hi_res &= ~(1 << 6); |
| 64 | uuid->clk_seq_hi_res |= 1 << 7; |
| 65 | } |