Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
Tor
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Benjamin J. Thompson
Tor
Commits
f07a5125
Commit
f07a5125
authored
12 years ago
by
Nick Mathewson
Browse files
Options
Downloads
Patches
Plain Diff
Implement a constant-time safe_mem_is_zero.
parent
92d6a83e
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/common/di_ops.c
+17
-0
17 additions, 0 deletions
src/common/di_ops.c
src/common/di_ops.h
+2
-0
2 additions, 0 deletions
src/common/di_ops.h
src/test/test_util.c
+10
-0
10 additions, 0 deletions
src/test/test_util.c
with
29 additions
and
0 deletions
src/common/di_ops.c
+
17
−
0
View file @
f07a5125
...
...
@@ -203,3 +203,20 @@ dimap_search(const di_digest256_map_t *map, const uint8_t *key,
return
(
void
*
)
result
;
}
/**
* Return true iff the <b>sz</b> bytes at <b>mem</b> are all zero. Runs in
* time independent of the contents of <b>mem</b>.
*/
int
safe_mem_is_zero
(
const
void
*
mem
,
size_t
sz
)
{
uint32_t
total
=
0
;
const
uint8_t
*
ptr
=
mem
;
while
(
sz
--
)
{
total
|=
*
ptr
++
;
}
return
1
&
((
total
-
1
)
>>
8
);
}
This diff is collapsed.
Click to expand it.
src/common/di_ops.h
+
2
−
0
View file @
f07a5125
...
...
@@ -27,6 +27,8 @@ int tor_memeq(const void *a, const void *b, size_t sz);
#define fast_memeq(a,b,c) (0==memcmp((a),(b),(c)))
#define fast_memneq(a,b,c) (0!=memcmp((a),(b),(c)))
int
safe_mem_is_zero
(
const
void
*
mem
,
size_t
sz
);
/** A type for a map from DIGEST256_LEN-byte blobs to void*, such that
* data lookups take an amount of time proportional only to the size
* of the map, and not to the position or presence of the item in the map.
...
...
This diff is collapsed.
Click to expand it.
src/test/test_util.c
+
10
−
0
View file @
f07a5125
...
...
@@ -2843,6 +2843,16 @@ test_util_di_ops(void)
test_eq
(
neq1
,
!
eq1
);
}
tt_int_op
(
1
,
==
,
safe_mem_is_zero
(
""
,
0
));
tt_int_op
(
1
,
==
,
safe_mem_is_zero
(
""
,
1
));
tt_int_op
(
0
,
==
,
safe_mem_is_zero
(
"a"
,
1
));
tt_int_op
(
0
,
==
,
safe_mem_is_zero
(
"a"
,
2
));
tt_int_op
(
0
,
==
,
safe_mem_is_zero
(
"
\0
a"
,
2
));
tt_int_op
(
1
,
==
,
safe_mem_is_zero
(
"
\0\0
a"
,
2
));
tt_int_op
(
1
,
==
,
safe_mem_is_zero
(
"
\0\0\0\0\0\0\0\0
"
,
8
));
tt_int_op
(
1
,
==
,
safe_mem_is_zero
(
"
\0\0\0\0\0\0\0\0
a"
,
8
));
tt_int_op
(
0
,
==
,
safe_mem_is_zero
(
"
\0\0\0\0\0\0\0\0
a"
,
9
));
done:
;
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment