Re: [U-Boot] [PATCH v4 3/6] lib: uuid: add functions to generate UUID version 4

2014-03-27 Thread Przemyslaw Marczak

Hi,

On 03/26/2014 07:47 PM, Stephen Warren wrote:

On 03/26/2014 06:00 AM, Przemyslaw Marczak wrote:

On 03/25/2014 08:28 PM, Stephen Warren wrote:

On 03/19/2014 11:58 AM, Przemyslaw Marczak wrote:

This patch adds support to generate UUID (Universally Unique Identifier)
in version 4 based on RFC4122, which is randomly.

Source: https://www.ietf.org/rfc/rfc4122.txt



diff --git a/lib/uuid.c b/lib/uuid.c



   /*
* UUID - Universally Unique IDentifier - 128 bits unique number.
*There are 5 versions and one variant of UUID defined by RFC4122
- *specification. Depends on version uuid number base on a time,
- *host name, MAC address or random data.
+ *specification. Depends on version uuid number base on:


I still have no idea what "Depends on version uuid number base on" means.


It means that each UUID version "result" depends on different source
data, as listed here...


How bout replacing that sentence with:

A UUID contains a set of fields. The set varies depending on the version
of the UUID, as shown below:


Ok, no problem.

+ *- time, MAC address(v1),
+ *- user ID(v2),
+ *- MD5 of name or URL(v3),
+ *- random data(v4),
+ *- SHA-1 of name or URL(v5),



+ * timestamp - 60-bit: time_low, time_mid, time_hi_and_version
+ * version   - 4 bit (bit 4 through 7 of the time_hi_and_version)
+ * clock seq - 14 bit: clock_seq_hi_and_reserved, clock_seq_low
+ * variant:  - bit 6 and 7 of clock_seq_hi_and_reserved
+ * node  - 48 bit
+ * In this version all fields beside 4 bit version are randomly generated.
+ * source: https://www.ietf.org/rfc/rfc4122.txt


gen_rand_uuid() doesn't actually honor that format; it creates pure
random data rather than filling in any timestamps, clock sequence
data, etc.


Actually, yes but two fields are NOT set randomly, and this is what
comment includes:
"In this version all fields beside 4 bit version are randomly generated."
Moreover the gen_rand_uuid() respects endianess for setting bits,
and this could be checked on linux host by "uuid -d uboot_uuid_string"
in shell.


While it's true that some fields are set non-randomly, most aren't; you
really can't claim that e.g. placing random data in the timestamp field
is a valid timestamp.

As I said before, I will remove the "version 4" from the comment head 
because the layout is valid for all uuid versions. So we can take into 
account the meaning of each field only after checking the version of 
uuid first.


Thanks
--
Przemyslaw Marczak
Samsung R&D Institute Poland
Samsung Electronics
p.marc...@samsung.com
___
U-Boot mailing list
U-Boot@lists.denx.de
http://lists.denx.de/mailman/listinfo/u-boot


Re: [U-Boot] [PATCH v4 3/6] lib: uuid: add functions to generate UUID version 4

2014-03-26 Thread Stephen Warren
On 03/26/2014 06:00 AM, Przemyslaw Marczak wrote:
> On 03/25/2014 08:28 PM, Stephen Warren wrote:
>> On 03/19/2014 11:58 AM, Przemyslaw Marczak wrote:
>>> This patch adds support to generate UUID (Universally Unique Identifier)
>>> in version 4 based on RFC4122, which is randomly.
>>>
>>> Source: https://www.ietf.org/rfc/rfc4122.txt

>>> diff --git a/lib/uuid.c b/lib/uuid.c
>>
>>>   /*
>>>* UUID - Universally Unique IDentifier - 128 bits unique number.
>>>*There are 5 versions and one variant of UUID defined by RFC4122
>>> - *specification. Depends on version uuid number base on a time,
>>> - *host name, MAC address or random data.
>>> + *specification. Depends on version uuid number base on:
>>
>> I still have no idea what "Depends on version uuid number base on" means.
> 
> It means that each UUID version "result" depends on different source
> data, as listed here...

How bout replacing that sentence with:

A UUID contains a set of fields. The set varies depending on the version
of the UUID, as shown below:

>>> + *- time, MAC address(v1),
>>> + *- user ID(v2),
>>> + *- MD5 of name or URL(v3),
>>> + *- random data(v4),
>>> + *- SHA-1 of name or URL(v5),

>>> + * timestamp - 60-bit: time_low, time_mid, time_hi_and_version
>>> + * version   - 4 bit (bit 4 through 7 of the time_hi_and_version)
>>> + * clock seq - 14 bit: clock_seq_hi_and_reserved, clock_seq_low
>>> + * variant:  - bit 6 and 7 of clock_seq_hi_and_reserved
>>> + * node  - 48 bit
>>> + * In this version all fields beside 4 bit version are randomly generated.
>>> + * source: https://www.ietf.org/rfc/rfc4122.txt
>>
>> gen_rand_uuid() doesn't actually honor that format; it creates pure
>> random data rather than filling in any timestamps, clock sequence
>> data, etc.
> 
> Actually, yes but two fields are NOT set randomly, and this is what
> comment includes:
> "In this version all fields beside 4 bit version are randomly generated."
> Moreover the gen_rand_uuid() respects endianess for setting bits,
> and this could be checked on linux host by "uuid -d uboot_uuid_string"
> in shell.

While it's true that some fields are set non-randomly, most aren't; you
really can't claim that e.g. placing random data in the timestamp field
is a valid timestamp.
___
U-Boot mailing list
U-Boot@lists.denx.de
http://lists.denx.de/mailman/listinfo/u-boot


Re: [U-Boot] [PATCH v4 3/6] lib: uuid: add functions to generate UUID version 4

2014-03-26 Thread Przemyslaw Marczak

Hello Stephen,

On 03/25/2014 08:28 PM, Stephen Warren wrote:

On 03/19/2014 11:58 AM, Przemyslaw Marczak wrote:

This patch adds support to generate UUID (Universally Unique Identifier)
in version 4 based on RFC4122, which is randomly.

Source: https://www.ietf.org/rfc/rfc4122.txt


Some nits in the comments below, but otherwise:
Acked-by: Stephen Warren 


diff --git a/include/uuid.h b/include/uuid.h



+/* This is structure is in big-endian */
+struct uuid {


Not any more; with the introduction of enum uuid_str_t, some of the
fields could be either LE or BE. I would say "See the comment near the
top of lib/uuid.c for details of the endianness of fields in this struct".



No, those fields are always in big-endian. So no matter what is 
architecture endianess - this data should be stored as big endian. Only 
string representation has different character order for GUID.



diff --git a/lib/uuid.c b/lib/uuid.c



  /*
   * UUID - Universally Unique IDentifier - 128 bits unique number.
   *There are 5 versions and one variant of UUID defined by RFC4122
- *specification. Depends on version uuid number base on a time,
- *host name, MAC address or random data.
+ *specification. Depends on version uuid number base on:


I still have no idea what "Depends on version uuid number base on" means.



It means that each UUID version "result" depends on different source 
data, as listed here...



+ *- time, MAC address(v1),
+ *- user ID(v2),
+ *- MD5 of name or URL(v3),
+ *- random data(v4),
+ *- SHA-1 of name or URL(v5),
+ *
+ * This library implements UUID v4.


I think that should say "gen_rand_uuid()" not "This library", since the
source of the data in the UUID fields only matters when creating the
UUID, not when performing str<->bin conversion.



Yes, right notice.


+ *
+ * Layout of UUID Version 4:


I should remove "Version 4" in the comment subject, because layout 
refers to all uuid versions.



+ * timestamp - 60-bit: time_low, time_mid, time_hi_and_version
+ * version   - 4 bit (bit 4 through 7 of the time_hi_and_version)
+ * clock seq - 14 bit: clock_seq_hi_and_reserved, clock_seq_low
+ * variant:  - bit 6 and 7 of clock_seq_hi_and_reserved
+ * node  - 48 bit
+ * In this version all fields beside 4 bit version are randomly generated.
+ * source: https://www.ietf.org/rfc/rfc4122.txt


gen_rand_uuid() doesn't actually honor that format; it creates pure
random data rather than filling in any timestamps, clock sequence data, etc.



Actually, yes but two fields are NOT set randomly, and this is what 
comment includes:

"In this version all fields beside 4 bit version are randomly generated."
Moreover the gen_rand_uuid() respects endianess for setting bits,
and this could be checked on linux host by "uuid -d uboot_uuid_string" 
in shell.


Thanks
--
Przemyslaw Marczak
Samsung R&D Institute Poland
Samsung Electronics
p.marc...@samsung.com
___
U-Boot mailing list
U-Boot@lists.denx.de
http://lists.denx.de/mailman/listinfo/u-boot


Re: [U-Boot] [PATCH v4 3/6] lib: uuid: add functions to generate UUID version 4

2014-03-25 Thread Stephen Warren
On 03/19/2014 11:58 AM, Przemyslaw Marczak wrote:
> This patch adds support to generate UUID (Universally Unique Identifier)
> in version 4 based on RFC4122, which is randomly.
> 
> Source: https://www.ietf.org/rfc/rfc4122.txt

Some nits in the comments below, but otherwise:
Acked-by: Stephen Warren 

> diff --git a/include/uuid.h b/include/uuid.h

> +/* This is structure is in big-endian */
> +struct uuid {

Not any more; with the introduction of enum uuid_str_t, some of the
fields could be either LE or BE. I would say "See the comment near the
top of lib/uuid.c for details of the endianness of fields in this struct".

> diff --git a/lib/uuid.c b/lib/uuid.c

>  /*
>   * UUID - Universally Unique IDentifier - 128 bits unique number.
>   *There are 5 versions and one variant of UUID defined by RFC4122
> - *specification. Depends on version uuid number base on a time,
> - *host name, MAC address or random data.
> + *specification. Depends on version uuid number base on:

I still have no idea what "Depends on version uuid number base on" means.

> + *- time, MAC address(v1),
> + *- user ID(v2),
> + *- MD5 of name or URL(v3),
> + *- random data(v4),
> + *- SHA-1 of name or URL(v5),
> + *
> + * This library implements UUID v4.

I think that should say "gen_rand_uuid()" not "This library", since the
source of the data in the UUID fields only matters when creating the
UUID, not when performing str<->bin conversion.

> + *
> + * Layout of UUID Version 4:
> + * timestamp - 60-bit: time_low, time_mid, time_hi_and_version
> + * version   - 4 bit (bit 4 through 7 of the time_hi_and_version)
> + * clock seq - 14 bit: clock_seq_hi_and_reserved, clock_seq_low
> + * variant:  - bit 6 and 7 of clock_seq_hi_and_reserved
> + * node  - 48 bit
> + * In this version all fields beside 4 bit version are randomly generated.
> + * source: https://www.ietf.org/rfc/rfc4122.txt

gen_rand_uuid() doesn't actually honor that format; it creates pure
random data rather than filling in any timestamps, clock sequence data, etc.
___
U-Boot mailing list
U-Boot@lists.denx.de
http://lists.denx.de/mailman/listinfo/u-boot


[U-Boot] [PATCH v4 3/6] lib: uuid: add functions to generate UUID version 4

2014-03-19 Thread Przemyslaw Marczak
This patch adds support to generate UUID (Universally Unique Identifier)
in version 4 based on RFC4122, which is randomly.

Source: https://www.ietf.org/rfc/rfc4122.txt

Changes:
- add new config: CONFIG_RANDOM_UUID: compile uuid.c and rand.c

lib/uuid.c:
- add gen_rand_uuid() - this function writes 16 bytes len binary representati
  UUID v4 to the memory at given address.

- add gen_rand_uuid_str() - this function writes 37 bytes len hexadecimal
  ASCII string representation of UUID v4 to the memory at given address.

Signed-off-by: Przemyslaw Marczak 
Cc: Stephen Warren 
Cc: Lukasz Majewski 
Cc: tr...@ti.com

---
Changes v2:
- put uuid generation changes in a separate commit
- get_uuid_str() - change name to gen_rand_uuid_str()
- add new function: gen_rand_uuid()
- remove unnecessary '\0' at the end of uuid string
- drop unnecessary error checking
- functions now takes pointers to allocated memory instead of alloc it itself
- add new config option: CONFIG_RANDOM_UUID

Changes v3:
- remove unused UUID_STR_BYTE_LEN
- reword comments
- remove null pointer checking from gen_rand_uuid() and gen_rand_uuid_str()
- remove unneeded memset from gen_rand_uuid()
- undo moving vsprintf.o object in lib/Makefile
- add attribute "packed" to the uuid structure
- gen_rand_uuid(): add endian functions for modify uuid data
- gen_rand_uuid(): use memcpy() to store uuid data into given buffer for avoi
  unaligned access issues
- change uuid version and variant masks to proper for use with clrsetbits_*
- add #ifdef CONFIG_RANDOM_UUID to random uuid code for avoid warnings

Changes v4:
- add new parameter to define UUID string format for UUID or GUID which differs
  in endianness of first three string blocks.
- add uuid structure and version 4 data to uuid header file
- lib/Makefile: add CONFIG_RAND_UUID dependency to rand.o and uuid.o
---
 include/common.h |  3 ++-
 include/uuid.h   | 22 ++-
 lib/Makefile |  2 ++
 lib/uuid.c   | 65 ++--
 4 files changed, 88 insertions(+), 4 deletions(-)

diff --git a/include/common.h b/include/common.h
index 658c326..deb08f2 100644
--- a/include/common.h
+++ b/include/common.h
@@ -830,7 +830,8 @@ char *  strmhz(char *buf, unsigned long hz);
 /* lib/rand.c */
 #if defined(CONFIG_RANDOM_MACADDR) || \
defined(CONFIG_BOOTP_RANDOM_DELAY) || \
-   defined(CONFIG_CMD_LINK_LOCAL)
+   defined(CONFIG_CMD_LINK_LOCAL) || \
+   defined(CONFIG_RANDOM_UUID)
 #define RAND_MAX -1U
 void srand(unsigned int seed);
 unsigned int rand(void);
diff --git a/include/uuid.h b/include/uuid.h
index 0d16038..32e592c 100644
--- a/include/uuid.h
+++ b/include/uuid.h
@@ -7,15 +7,35 @@
 #ifndef __UUID_H__
 #define __UUID_H__
 
+/* This is structure is in big-endian */
+struct uuid {
+   unsigned int time_low;
+   unsigned short time_mid;
+   unsigned short time_hi_and_version;
+   unsigned char clock_seq_hi_and_reserved;
+   unsigned char clock_seq_low;
+   unsigned char node[6];
+} __packed;
+
 typedef enum {
UUID_STR_FORMAT_STD,
UUID_STR_FORMAT_GUID
 } uuid_str_t;
 
 #define UUID_STR_LEN   36
-#define UUID_BIN_LEN   16
+#define UUID_BIN_LEN   sizeof(struct uuid)
+
+#define UUID_VERSION_MASK  0xf000
+#define UUID_VERSION_SHIFT 12
+#define UUID_VERSION   0x4
+
+#define UUID_VARIANT_MASK  0xc0
+#define UUID_VARIANT_SHIFT 7
+#define UUID_VARIANT   0x1
 
 int uuid_str_valid(const char *uuid);
 int uuid_str_to_bin(char *uuid_str, unsigned char *uuid_bin, uuid_str_t 
format);
 void uuid_bin_to_str(unsigned char *uuid_bin, char *uuid_str, uuid_str_t 
format);
+void gen_rand_uuid(unsigned char *uuid_bin);
+void gen_rand_uuid_str(char *uuid_str, uuid_str_t format);
 #endif
diff --git a/lib/Makefile b/lib/Makefile
index 2e8bd20..fd75e80 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -63,6 +63,8 @@ obj-$(CONFIG_TRACE) += trace.o
 obj-$(CONFIG_BOOTP_PXE) += uuid.o
 obj-$(CONFIG_PARTITION_UUIDS) += uuid.o
 obj-y += vsprintf.o
+obj-$(CONFIG_RANDOM_UUID) += uuid.o
+obj-$(CONFIG_RANDOM_UUID) += rand.o
 obj-$(CONFIG_RANDOM_MACADDR) += rand.o
 obj-$(CONFIG_BOOTP_RANDOM_DELAY) += rand.o
 obj-$(CONFIG_CMD_LINK_LOCAL) += rand.o
diff --git a/lib/uuid.c b/lib/uuid.c
index 75a5608..d3ba60e 100644
--- a/lib/uuid.c
+++ b/lib/uuid.c
@@ -14,8 +14,23 @@
 /*
  * UUID - Universally Unique IDentifier - 128 bits unique number.
  *There are 5 versions and one variant of UUID defined by RFC4122
- *specification. Depends on version uuid number base on a time,
- *host name, MAC address or random data.
+ *specification. Depends on version uuid number base on:
+ *- time, MAC address(v1),
+ *- user ID(v2),
+ *- MD5 of name or URL(v3),
+ *- random data(v4),
+ *- SHA-1 of name or URL(v5),
+ *
+ * This library implements UUID v4.
+ *
+ * Layout of UUID Version 4:
+ * timestamp - 60-bit: time_low, time_m