lecture 12 - integerself.cs.pub.ro/cns/res/lectures/lecture-12.pdf · cse dep, acs, upb lecture 12,...

40
Lecture 12 Integers Computer and Network Security 19th of December 2016 Computer Science and Engineering Department CSE Dep, ACS, UPB Lecture 12, Integers 1/40

Upload: others

Post on 30-Sep-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 12 - Integerself.cs.pub.ro/cns/res/lectures/lecture-12.pdf · CSE Dep, ACS, UPB Lecture 12, Integers 9/40. Signed-magnitude for Negative Integers I direct representation I

Lecture 12Integers

Computer and Network Security19th of December 2016

Computer Science and Engineering Department

CSE Dep, ACS, UPB Lecture 12, Integers 1/40

Page 2: Lecture 12 - Integerself.cs.pub.ro/cns/res/lectures/lecture-12.pdf · CSE Dep, ACS, UPB Lecture 12, Integers 9/40. Signed-magnitude for Negative Integers I direct representation I

Outline

Data Types

Representation

Conversions

Operations

Issues

Rules and Recommendations

Conclusion

CSE Dep, ACS, UPB Lecture 12, Integers 2/40

Page 3: Lecture 12 - Integerself.cs.pub.ro/cns/res/lectures/lecture-12.pdf · CSE Dep, ACS, UPB Lecture 12, Integers 9/40. Signed-magnitude for Negative Integers I direct representation I

Integers

I numbers

I non-floating point

I positive, negative or zero

CSE Dep, ACS, UPB Lecture 12, Integers 3/40

Page 4: Lecture 12 - Integerself.cs.pub.ro/cns/res/lectures/lecture-12.pdf · CSE Dep, ACS, UPB Lecture 12, Integers 9/40. Signed-magnitude for Negative Integers I direct representation I

Integer Use

I actual numbers and integers values and . . .

I array index

I length/size of an object

I pointer arithmetic

I loop counter, array bound

I argument to a memory allocation function

CSE Dep, ACS, UPB Lecture 12, Integers 4/40

Page 5: Lecture 12 - Integerself.cs.pub.ro/cns/res/lectures/lecture-12.pdf · CSE Dep, ACS, UPB Lecture 12, Integers 9/40. Signed-magnitude for Negative Integers I direct representation I

Data Types

I char

I short

I int

I long

I long long

I pointer

CSE Dep, ACS, UPB Lecture 12, Integers 5/40

Page 6: Lecture 12 - Integerself.cs.pub.ro/cns/res/lectures/lecture-12.pdf · CSE Dep, ACS, UPB Lecture 12, Integers 9/40. Signed-magnitude for Negative Integers I direct representation I

Size of Data Types. Data Models

CERT C Programming Language Secure Coding Standard, pg. 118

CSE Dep, ACS, UPB Lecture 12, Integers 6/40

Page 7: Lecture 12 - Integerself.cs.pub.ro/cns/res/lectures/lecture-12.pdf · CSE Dep, ACS, UPB Lecture 12, Integers 9/40. Signed-magnitude for Negative Integers I direct representation I

To Bear in Mind

I representation of data types

I bounds (minimum, maximum)

I signedness

I conversion

I pointers and integers

CSE Dep, ACS, UPB Lecture 12, Integers 7/40

Page 8: Lecture 12 - Integerself.cs.pub.ro/cns/res/lectures/lecture-12.pdf · CSE Dep, ACS, UPB Lecture 12, Integers 9/40. Signed-magnitude for Negative Integers I direct representation I

Outline

Data Types

Representation

Conversions

Operations

Issues

Rules and Recommendations

Conclusion

CSE Dep, ACS, UPB Lecture 12, Integers 8/40

Page 9: Lecture 12 - Integerself.cs.pub.ro/cns/res/lectures/lecture-12.pdf · CSE Dep, ACS, UPB Lecture 12, Integers 9/40. Signed-magnitude for Negative Integers I direct representation I

Positive Integers

I always the same

I first bit is 0 for signed integers and the rest is the value

I the actual value is stored for unsigned integers

I representation differs for (signed) negative integers

CSE Dep, ACS, UPB Lecture 12, Integers 9/40

Page 10: Lecture 12 - Integerself.cs.pub.ro/cns/res/lectures/lecture-12.pdf · CSE Dep, ACS, UPB Lecture 12, Integers 9/40. Signed-magnitude for Negative Integers I direct representation I

Signed-magnitude for Negative Integers

I direct representation

I first bit is 1

I the rest of the bits are the actual value

I issue: difficult hardware implementation

CSE Dep, ACS, UPB Lecture 12, Integers 10/40

Page 11: Lecture 12 - Integerself.cs.pub.ro/cns/res/lectures/lecture-12.pdf · CSE Dep, ACS, UPB Lecture 12, Integers 9/40. Signed-magnitude for Negative Integers I direct representation I

1’s Complement for Negative Integers

I first bit is 1

I the rest of the bits are complemented (reversed)

I the circuitry is simpler

I issue: two values for zero (positive and negative zero)

CSE Dep, ACS, UPB Lecture 12, Integers 11/40

Page 12: Lecture 12 - Integerself.cs.pub.ro/cns/res/lectures/lecture-12.pdf · CSE Dep, ACS, UPB Lecture 12, Integers 9/40. Signed-magnitude for Negative Integers I direct representation I

2’s Complement for Negative Integers

I first bit is 1

I the rest of the bits are complemented and added one

I a single (positive) representation for zero

CSE Dep, ACS, UPB Lecture 12, Integers 12/40

Page 13: Lecture 12 - Integerself.cs.pub.ro/cns/res/lectures/lecture-12.pdf · CSE Dep, ACS, UPB Lecture 12, Integers 9/40. Signed-magnitude for Negative Integers I direct representation I

Signed/Unsigned Representation and Ranges

I we use 2’s complement

I consider N bits width representation

I 2N possible values

I for unsigned integers range is (0. . . 2N − 1)

I for signed integers, range is (−2N−1 . . . 2N−1 − 1)

CSE Dep, ACS, UPB Lecture 12, Integers 13/40

Page 14: Lecture 12 - Integerself.cs.pub.ro/cns/res/lectures/lecture-12.pdf · CSE Dep, ACS, UPB Lecture 12, Integers 9/40. Signed-magnitude for Negative Integers I direct representation I

Outline

Data Types

Representation

Conversions

Operations

Issues

Rules and Recommendations

Conclusion

CSE Dep, ACS, UPB Lecture 12, Integers 14/40

Page 15: Lecture 12 - Integerself.cs.pub.ro/cns/res/lectures/lecture-12.pdf · CSE Dep, ACS, UPB Lecture 12, Integers 9/40. Signed-magnitude for Negative Integers I direct representation I

Promotions

I integers smaller than int are promoted to int when performingoperations

I done to avoid overflows

I char c1, c2; c1 = c1 + c2;

CSE Dep, ACS, UPB Lecture 12, Integers 15/40

Page 16: Lecture 12 - Integerself.cs.pub.ro/cns/res/lectures/lecture-12.pdf · CSE Dep, ACS, UPB Lecture 12, Integers 9/40. Signed-magnitude for Negative Integers I direct representation I

Types and Ranks

I type: signed or unsigned

I ranks: long long > long > int > short > char

I convert to the larger rank

CSE Dep, ACS, UPB Lecture 12, Integers 16/40

Page 17: Lecture 12 - Integerself.cs.pub.ro/cns/res/lectures/lecture-12.pdf · CSE Dep, ACS, UPB Lecture 12, Integers 9/40. Signed-magnitude for Negative Integers I direct representation I

Unsigned to Unsigned

I safe from shorter range to larger range

I truncation if converting from larger range to shorter range

CSE Dep, ACS, UPB Lecture 12, Integers 17/40

Page 18: Lecture 12 - Integerself.cs.pub.ro/cns/res/lectures/lecture-12.pdf · CSE Dep, ACS, UPB Lecture 12, Integers 9/40. Signed-magnitude for Negative Integers I direct representation I

Unsigned to Signed

I same rank

I high-order bit is signed bit

I sign may change

CSE Dep, ACS, UPB Lecture 12, Integers 18/40

Page 19: Lecture 12 - Integerself.cs.pub.ro/cns/res/lectures/lecture-12.pdf · CSE Dep, ACS, UPB Lecture 12, Integers 9/40. Signed-magnitude for Negative Integers I direct representation I

Signed to Signed

I sign-extension is convert to larger rank

I preserve lower part in case of conversion to a smaller rank

CSE Dep, ACS, UPB Lecture 12, Integers 19/40

Page 20: Lecture 12 - Integerself.cs.pub.ro/cns/res/lectures/lecture-12.pdf · CSE Dep, ACS, UPB Lecture 12, Integers 9/40. Signed-magnitude for Negative Integers I direct representation I

Signed to Unsigned

I value unchanged if signed integer is positive

I truncation if unsigned rank is smaller

I sign-extension if larger unsigned rank

I in case of negative integers, a large positive integers isobtained

CSE Dep, ACS, UPB Lecture 12, Integers 20/40

Page 21: Lecture 12 - Integerself.cs.pub.ro/cns/res/lectures/lecture-12.pdf · CSE Dep, ACS, UPB Lecture 12, Integers 9/40. Signed-magnitude for Negative Integers I direct representation I

Signed/Unsigned Characters

I char may be signed char on unsigned char

I when storing negative signed char in an integer, the result is alarge negative number

I if value above 127 (0x7F) use unsigned char

CSE Dep, ACS, UPB Lecture 12, Integers 21/40

Page 22: Lecture 12 - Integerself.cs.pub.ro/cns/res/lectures/lecture-12.pdf · CSE Dep, ACS, UPB Lecture 12, Integers 9/40. Signed-magnitude for Negative Integers I direct representation I

In a Nutshell

CSE Dep, ACS, UPB Lecture 12, Integers 22/40

Page 23: Lecture 12 - Integerself.cs.pub.ro/cns/res/lectures/lecture-12.pdf · CSE Dep, ACS, UPB Lecture 12, Integers 9/40. Signed-magnitude for Negative Integers I direct representation I

Outline

Data Types

Representation

Conversions

Operations

Issues

Rules and Recommendations

Conclusion

CSE Dep, ACS, UPB Lecture 12, Integers 23/40

Page 24: Lecture 12 - Integerself.cs.pub.ro/cns/res/lectures/lecture-12.pdf · CSE Dep, ACS, UPB Lecture 12, Integers 9/40. Signed-magnitude for Negative Integers I direct representation I

Addition

I integer and integer

I pointer and integer

I may result in overflow

I arithmetic conversion

CSE Dep, ACS, UPB Lecture 12, Integers 24/40

Page 25: Lecture 12 - Integerself.cs.pub.ro/cns/res/lectures/lecture-12.pdf · CSE Dep, ACS, UPB Lecture 12, Integers 9/40. Signed-magnitude for Negative Integers I direct representation I

Subtraction

I integer and integer

I pointer and integer

I may result in negative overflow

CSE Dep, ACS, UPB Lecture 12, Integers 25/40

Page 26: Lecture 12 - Integerself.cs.pub.ro/cns/res/lectures/lecture-12.pdf · CSE Dep, ACS, UPB Lecture 12, Integers 9/40. Signed-magnitude for Negative Integers I direct representation I

Multiplication

I prone to overflow even for small operands

I good idea to allocate twice the storage for the product

CSE Dep, ACS, UPB Lecture 12, Integers 26/40

Page 27: Lecture 12 - Integerself.cs.pub.ro/cns/res/lectures/lecture-12.pdf · CSE Dep, ACS, UPB Lecture 12, Integers 9/40. Signed-magnitude for Negative Integers I direct representation I

Division

I division by zero

I division of minimum signed integer value divided by -1

CSE Dep, ACS, UPB Lecture 12, Integers 27/40

Page 28: Lecture 12 - Integerself.cs.pub.ro/cns/res/lectures/lecture-12.pdf · CSE Dep, ACS, UPB Lecture 12, Integers 9/40. Signed-magnitude for Negative Integers I direct representation I

Modulo

I similar to division, since division is involved

I careful at negative remainders

CSE Dep, ACS, UPB Lecture 12, Integers 28/40

Page 29: Lecture 12 - Integerself.cs.pub.ro/cns/res/lectures/lecture-12.pdf · CSE Dep, ACS, UPB Lecture 12, Integers 9/40. Signed-magnitude for Negative Integers I direct representation I

Shifting

I careful when shifting signed negative integers

I integer promotion must be taken into consideration

CSE Dep, ACS, UPB Lecture 12, Integers 29/40

Page 30: Lecture 12 - Integerself.cs.pub.ro/cns/res/lectures/lecture-12.pdf · CSE Dep, ACS, UPB Lecture 12, Integers 9/40. Signed-magnitude for Negative Integers I direct representation I

Outline

Data Types

Representation

Conversions

Operations

Issues

Rules and Recommendations

Conclusion

CSE Dep, ACS, UPB Lecture 12, Integers 30/40

Page 31: Lecture 12 - Integerself.cs.pub.ro/cns/res/lectures/lecture-12.pdf · CSE Dep, ACS, UPB Lecture 12, Integers 9/40. Signed-magnitude for Negative Integers I direct representation I

Truncation

I convert to a smaller integer type

I the initial value is outside range

I high order bits are lost in case of converting from larger toshorter

CSE Dep, ACS, UPB Lecture 12, Integers 31/40

Page 32: Lecture 12 - Integerself.cs.pub.ro/cns/res/lectures/lecture-12.pdf · CSE Dep, ACS, UPB Lecture 12, Integers 9/40. Signed-magnitude for Negative Integers I direct representation I

Overflow

I may be signed on usigned

I go beyond maximum value or below minimum value

CSE Dep, ACS, UPB Lecture 12, Integers 32/40

Page 33: Lecture 12 - Integerself.cs.pub.ro/cns/res/lectures/lecture-12.pdf · CSE Dep, ACS, UPB Lecture 12, Integers 9/40. Signed-magnitude for Negative Integers I direct representation I

Sign Error

I convert unsigned to signedI if “bit sign” in integer is set it results in a negative number

I convert signed to unsignedI negative numbers result in large positive numbers

CSE Dep, ACS, UPB Lecture 12, Integers 33/40

Page 34: Lecture 12 - Integerself.cs.pub.ro/cns/res/lectures/lecture-12.pdf · CSE Dep, ACS, UPB Lecture 12, Integers 9/40. Signed-magnitude for Negative Integers I direct representation I

Negative Indices

I negative arrays index

I value outside bounds

CSE Dep, ACS, UPB Lecture 12, Integers 34/40

Page 35: Lecture 12 - Integerself.cs.pub.ro/cns/res/lectures/lecture-12.pdf · CSE Dep, ACS, UPB Lecture 12, Integers 9/40. Signed-magnitude for Negative Integers I direct representation I

Outline

Data Types

Representation

Conversions

Operations

Issues

Rules and Recommendations

Conclusion

CSE Dep, ACS, UPB Lecture 12, Integers 35/40

Page 36: Lecture 12 - Integerself.cs.pub.ro/cns/res/lectures/lecture-12.pdf · CSE Dep, ACS, UPB Lecture 12, Integers 9/40. Signed-magnitude for Negative Integers I direct representation I

Recommendations

I INT00-C to INT18-C on “04. Integers (INT)” in CERT CSecure Coding Standard

CSE Dep, ACS, UPB Lecture 12, Integers 36/40

Page 37: Lecture 12 - Integerself.cs.pub.ro/cns/res/lectures/lecture-12.pdf · CSE Dep, ACS, UPB Lecture 12, Integers 9/40. Signed-magnitude for Negative Integers I direct representation I

Rules

I INT30-C to INT36-C on “04. Integers (INT)” in CERT CSecure Coding Standard

CSE Dep, ACS, UPB Lecture 12, Integers 37/40

Page 38: Lecture 12 - Integerself.cs.pub.ro/cns/res/lectures/lecture-12.pdf · CSE Dep, ACS, UPB Lecture 12, Integers 9/40. Signed-magnitude for Negative Integers I direct representation I

Outline

Data Types

Representation

Conversions

Operations

Issues

Rules and Recommendations

Conclusion

CSE Dep, ACS, UPB Lecture 12, Integers 38/40

Page 39: Lecture 12 - Integerself.cs.pub.ro/cns/res/lectures/lecture-12.pdf · CSE Dep, ACS, UPB Lecture 12, Integers 9/40. Signed-magnitude for Negative Integers I direct representation I

Keywords

I integer

I type

I long long, long, int, short,char

I signed, unsigned

I bounds/range

I 2’s complement

I conversion

I promotion

I rank

I addition

I subtraction

I multiplication

I division

I modulo

I truncation

I overflow

I sign error

I negative index

CSE Dep, ACS, UPB Lecture 12, Integers 39/40

Page 40: Lecture 12 - Integerself.cs.pub.ro/cns/res/lectures/lecture-12.pdf · CSE Dep, ACS, UPB Lecture 12, Integers 9/40. Signed-magnitude for Negative Integers I direct representation I

References

I CERT C Secure Coding Standard – 04. Integers (INT) –https://www.securecoding.cert.org/confluence/

pages/viewpage.action?pageId=270

I Secure Coding in C and C++ ClassI Module 3. Integers

I Secure Coding in C and C++I Chapter 5. Integer Security

CSE Dep, ACS, UPB Lecture 12, Integers 40/40